package lk.mrt.cse.wihidum.client;

import org.apache.log4j.Logger;

import java.util.Collections.*;
import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * User: Hasini
 * Date: Mar 30, 2010
 * Time: 8:53:48 AM
 * To change this template use File | Settings | File Templates.
 */
public class BrokerList
{
        private static final Logger logger = Logger.getLogger (BrokerList.class);
        private static ArrayList<String> brokerList=new ArrayList<String>();
        private static int topBroker=0;
        public static void shuffleBrokerList()
        {
            Collections.shuffle(brokerList);
            logger.info("Top broker after shuffling is"+brokerList.get(0));

        }
        public static String getTopBroker()
        {
            String brokerAddress=brokerList.get(topBroker);
            if(topBroker==(brokerList.size()-1))
            {
               topBroker=0;
            }
            else if(topBroker<(brokerList.size()-1))
            {
               topBroker++;
            }

            return brokerAddress;
        }
        public static void addBrokerToList(String broker)
        {
            brokerList.add(broker);
            int size=brokerList.size();
            logger.info("Size of the broker list in client: "+size);
        }
        public static void removeBrokerFromList(String brokerAddress)
        {
          Iterator<String> it=brokerList.iterator();
           while(it.hasNext())
           {
               String currentBroker=it.next();
               if(currentBroker.equals(brokerAddress))
               {
                   brokerList.remove(brokerAddress);

               }
           }
        }
        public static boolean isEmpty()
        {
            boolean empty=false;
            empty=brokerList.isEmpty();
            return empty;
        }
        public static void clearBrokerList()
        {
            brokerList.clear();
        }

}
